package com.jhws.business.card.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.card.bo.CardDoorDevice;
import com.jhws.business.card.service.ITblCardDimService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/cardDoorDevice")
public class CardDoorDeviceCtrl {

    @Resource
    ITblCardDimService ICardService;

    @RequestMapping("saveBindings.x")
    @ResponseBody
    public JsonRespData saveBindings(@RequestBody CardDoorDevice [] cardDoorDevices){
        for(CardDoorDevice cardDoorDevice : cardDoorDevices){
            if(StringUtils.isNotEmpty(cardDoorDevice.getDevice()) && StringUtils.isNotEmpty(cardDoorDevice.getCardDeviceNumber()))
                ICardService.save(cardDoorDevice);
            else if(StringUtils.isEmpty(cardDoorDevice.getDevice()) && cardDoorDevice.getId() != null)
                ICardService.removeCardDevice(cardDoorDevice.getId());
        }

        return new JsonRespData("",1,"");
    }

    @RequestMapping("getBindings.x")
    @ResponseBody
    public JsonRespData getBindings(String unitStr){
        return new JsonRespData("",1,ICardService.findCardDoorDevsByWhere("unit = '"+unitStr+"'"));
    }
}
